package jiejie.jianzhioffer;

import java.util.HashMap;

public class offer07 {
    static int[] po;
    static HashMap<Integer,Integer> map = new HashMap<>();
    public static void main(String[] args) {

    }

    /**
     * 重建二叉树
     * @param preorder
     * @param inorder
     * @return
     */
    public static TreeNode buildTree(int[] preorder, int[] inorder) {
        po=preorder;
        for(int i=0; i<inorder.length; i++){
            map.put(inorder[i],i);
        }
        return bt(0,0,inorder.length-1);
    }
    public static TreeNode bt(int pleft, int inleft, int inright){
        if(inleft>inright)
            return null;
        TreeNode root=new TreeNode(po[pleft]);
        int i=map.get(po[pleft]);
        root.left=bt(pleft+1,inleft,i-1);
        root.right=bt(pleft+i-inleft+1,i+1,inright);
        return root;
    }

}